package com.example.controller;


import com.example.config.Utils.MedInfoInsertUtil;
import com.example.entity.MI_Table;
import com.example.entity.RespBean;
import com.example.entity.Result;
import com.example.mapper.MedicineInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liu
 * @since 2022-03-19
 */
@Api(tags = "MedicineInfoController")
@RestController
@ResponseBody
public class MedicineInfoController {

    @Autowired
    private MedicineInfoMapper medicineInfoMapper;

    @Autowired
    private MedInfoInsertUtil medInfoInsertUtil;

    @ApiOperation(value = "上传药品信息（一次仅插入一条数据）")
    @PostMapping(value = "/insertMedInfoOne")
    public void uploadMedInfo(@RequestBody MI_Table medicineInfo){
        medInfoInsertUtil.addMedInfo(medicineInfo);
    }
//    @GetMapping(value = "/query")
//    public MI_Table query(){
//        System.out.println(medicineInfoMapper.query()==null);
//        return medicineInfoMapper.query();
//    }

    @ApiOperation(value = "删除药品信息（一次仅删除一条数据）")
    @PostMapping(value = "/delMedInfoOne/{m_id}&{userid}")
    public RespBean deleteMedInfo(@PathVariable int m_id,@PathVariable int userid){

        System.out.println("delMedInfoOne: the m_id,userid is "+m_id+userid);

        if(m_id != 0){
            medicineInfoMapper.deleteMedInfoById(m_id,userid);
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("无m_id,删除失败");
        }

    }

    @ApiOperation(value = "删除药品信息（批量删除数据）")
    @PostMapping(value = "/delMedInfoSome/{userid}&{outMedList}")
    public RespBean deleteSomeMedInfo(@PathVariable Integer[] outMedList, @PathVariable int userid){
//        System.out.println("delMedInfoOne: the userid is "+userid);
        if(outMedList.length != 0){
            for (int i = 0; i < outMedList.length; i++) {
                System.out.println(outMedList[i]);
                medicineInfoMapper.deleteMedInfoById(outMedList[i],userid);
            }

            return RespBean.success("删除成功");
        }else {
            return RespBean.error("无m_id,删除失败");
        }

    }

    @GetMapping(value = "/select")
    Result select(){
        return Result.getSuccess().setData(medicineInfoMapper.queryBacthNumByName("1"));
    }


}
